package com.hckj.medical.controller;


import com.hckj.medical.entity.DoctorMsg;
import com.hckj.medical.service.IDoctorMsgService;
import com.hckj.medical.utils.Code;
import com.hckj.medical.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hckj
 * @since 2023-11-25
 */
@RestController
@RequestMapping("/medical/doctor-msg")
public class DoctorMsgController {
    @Autowired
    private IDoctorMsgService doctorMsgService;
    @PostMapping("/login")
    public R login(@RequestBody DoctorMsg doctorMsg){
        DoctorMsg doctorMsg1 = doctorMsgService.selectByphone(doctorMsg.getUsername());
        if (doctorMsg1 != null && doctorMsg1.getPassword().equals(doctorMsg.getPassword())){
            return new R(Code.GET_OK,doctorMsg1,"登录成功");
        }else if (doctorMsg1 != null && doctorMsg.getPassword() != doctorMsg1.getPassword()){
            return new R(Code.GET_ERRO,"密码错误");
        }else {
            return new R(Code.GET_ERRO,"用户名不存在");
        }
    }

    /**
     * 查询所有的医生
     * @param list
     * @return
     */
    @PostMapping("/findAllDoctor")
    public R findAllDoctor(@RequestBody List<Integer> list){
        List<DoctorMsg> doctorMsgs = doctorMsgService.selectAllDoctor(list);
        if (!doctorMsgs.isEmpty()){
            return new R(Code.GET_OK,doctorMsgs,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

    /**
     * 根据id查询医生的个人信息
     * @param id
     * @return
     */
    @GetMapping("/findbyid/{id}")
    public R selectBydid(@PathVariable Integer id){
        DoctorMsg byid = doctorMsgService.findByid(id);
        if (byid != null){
            return new R(Code.GET_OK,byid,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

    /**
     * 修改密码
     * @param modify
     * @return
     */
    @PostMapping("/findIdandPwd")
    public R selectBypwd(@RequestBody Map modify){
        Integer id = Integer.parseInt(modify.get("id").toString());
        DoctorMsg byid = doctorMsgService.findByid(id);
        String bpwd = modify.get("bpwd").toString();
        if (bpwd.equals(byid.getPassword())){
            DoctorMsg doctorMsg = new DoctorMsg();
            doctorMsg.setId(id);
            doctorMsg.setPassword(modify.get("endpwd").toString());
            doctorMsgService.updateByid(doctorMsg);
            return new R(Code.UPDATE_OK,"修改成功");
        }else {
            return new R(Code.ALL_ERRO,"旧密码错误，请重新输入");
        }
    }

    @PostMapping("/updateDoctorMsg")
    public R updateDoctor(@RequestBody DoctorMsg doctorMsg){
        doctorMsgService.updateByid(doctorMsg);
        return new R(Code.UPDATE_OK,"修改成功");
    }

}
